
from xml.dom.minidom import parse

def makeHTML():
	html = ""
	
	html += "<html>\n"
	html += "\t<head>\n"
	html += "\t\t<title>nanodoc</title>\n"
	html += "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"nanodoc.css\" />\n"
	html += "\t\t<script type=\"text/javascript\" src=\"nanodoc.js\"></script>"
	html += "\t</head>\n"
	html += "\t<body>\n"
	html += "\t\t<div id=\"nanohead\"><div id=\"nanotitle\">[ Retro Game System ]</div></div>\n"
	html += "\t\t<div id=\"nanobody\">\n"
	
	sourceXML = open("rgs.xml")
	dom = parse(sourceXML)
	sourceXML.close()

	html += "<div id=\"packages\">"
	html += "<div id=\"packagestitle\">Packages</div>"
	html += "<div id=\"nanobutton\">"
	html += "<ul>"
	
	for item in dom.documentElement.getElementsByTagName('package'):
		html += "<li><a href=\"javascript:showModules('%s_modules')\">%s</a></li>" % (item.attributes["name"].value.strip(" ").lower(), item.attributes["name"].value)

	html += "</ul>"
	html += "</div>"
	html += "</div>"

	html += "\n"
	for item in dom.documentElement.getElementsByTagName('package'):
		html += "<div style=\"display:none\" class=\"modules\" id=\"%s_modules\">" % (item.attributes["name"].value.strip(" ").lower())
		html += "<div id=\"modulestitle\">%s modules </div>" % (item.attributes["name"].value)
		html += "<div id=\"nanobutton\">"
		html += "<ul>"
		for moduleitem in item.getElementsByTagName('module'):
			html += "<li><a href=\"javascript:showModuleContents('%s_content')\">%s</a></li>" % (moduleitem.attributes["name"].value.strip(" ").lower(), moduleitem.attributes["name"].value)
		html += "</ul>"
		html += "</div>"
		html += "</div>"
	
	html += "\n"
	for item in dom.documentElement.getElementsByTagName('module'):
		html += "<div style=\"display:none\" class=\"modulelist\" id=\"%s_content\">" % (item.attributes["name"].value.strip(" ").lower())
		html += "<div id=\"modulestitle\">%s</div>" % (item.attributes["name"].value)

		if len(item.getElementsByTagName('struct')) > 0:
			html += "<div id=\"modulelisttitle2\">Structures</div>"
			html += "<div id=\"nanobutton\">"
			html += "<ul>"
			for subitem in item.getElementsByTagName('struct'):
				html += "<li><a href=\"javascript:showSubContents('%s_sub')\">%s</a></li>" % (subitem.attributes["name"].value.strip(" ").lower(), subitem.attributes["name"].value)
			html += "</ul>"
			html += "</div>"

		if len(item.getElementsByTagName('function')) > 0:
			html += "<div id=\"modulelisttitle2\">Functions</div>"
			html += "<div id=\"nanobutton\">"
			html += "<ul>"
			for subitem in item.getElementsByTagName('function'):
				html += "<li><a href=\"javascript:showSubContents('%s_sub')\">%s</a></li>" % (subitem.attributes["name"].value.strip(" ").lower(), subitem.attributes["name"].value)
			html += "</ul>"
			html += "</div>"
		html += "</div>"

	html += "\n"
	for item in dom.documentElement.getElementsByTagName('function'):
		html += "<div style=\"display:none\" class=\"moduledetail\" id=\"%s_sub\">" % (item.attributes["name"].value.strip(" ").lower())
		html +=	"<div id=\"moduledetailtitle\">%s</div>" % (item.attributes["name"].value)
		if item.attributes["retPtr"].value == "True":
			ptrFlag = "*"
		else:
			ptrFlag = ""
		html += "<div id=\"detailcontent\"> <span class=\"hl_type\">%s</span> %s<span class=\"hl_func\">%s </span>" % (item.attributes["type"].value, ptrFlag, item.attributes["name"].value)
		html += "("
		paramdescs = ""
		if len(item.getElementsByTagName('param')) > 0:
			params = []
			for subitem in item.getElementsByTagName('param'):
				if subitem.attributes["isPtr"].value == "True":
					ptrFlag = "*"
				else:
					ptrFlag = ""
				params.append("<span class=\"hl_type\">%s</span> %s<span class=\"hl_param\">%s</span>" % (subitem.attributes["type"].value, ptrFlag, subitem.attributes["name"].value))
				paramdescs += "<div class=\"hl_type_b\">%s</div><div class=\"ptr\">%s</div><div class=\"hl_param_b\">%s</div><div class=\"hl_paramdesc\">%s</div>" % (subitem.attributes["type"].value, ptrFlag, subitem.attributes["name"].value, subitem.childNodes[0].data)
			html += ", ".join(params)
		html += ")</div>"
		
		if len(item.getElementsByTagName('desc')) > 0:
			html +=	"<div id=\"detailcontent\"><div class=\"detailboxdesc\"> Description</div> <div class=\"detailbox\">%s.</div></div>" % (item.getElementsByTagName('desc')[0].childNodes[0].data)
	
		if len(item.getElementsByTagName('return')) > 0:
			html +=	"<div id=\"detailcontent\"><div class=\"detailboxdesc\"> Return value</div> <div class=\"detailbox\">%s.</div></div>" % (item.getElementsByTagName('return')[0].childNodes[0].data)
	
		if len(paramdescs) > 0:
			html += "<div id=\"detailcontent\"><div class=\"detailboxdesc\"> Parameters</div><div class=\"detailbox\">"
			html += paramdescs
			html += "</div></div>"
		html += "</div>"


	html += "\n"
	for item in dom.documentElement.getElementsByTagName('struct'):
		html += "<div style=\"display:none\" class=\"moduledetail\" id=\"%s_sub\">" % (item.attributes["name"].value.strip(" ").lower())
		html +=	"<div id=\"moduledetailtitle\">%s</div>" % (item.attributes["name"].value)
		html += "<div id=\"detailcontent\"> <span class=\"hl_type\">typedef struct</span> {<br/>"
		paramdescs = ""
		if len(item.getElementsByTagName('param')) > 0:
			params = []
			for subitem in item.getElementsByTagName('param'):
				if subitem.attributes["isPtr"].value == "True":
					ptrFlag = "*"
				else:
					ptrFlag = ""
				params.append("<span class=\"hl_type\">%s</span> %s<span class=\"hl_param\">%s</span>" % (subitem.attributes["type"].value, ptrFlag, subitem.attributes["name"].value))
				paramdescs += "<div class=\"hl_type_b\">%s</div><div class=\"ptr\">%s</div><div class=\"hl_param_b\">%s</div><div class=\"hl_paramdesc\">%s</div>" % (subitem.attributes["type"].value, ptrFlag, subitem.attributes["name"].value, subitem.childNodes[0].data)
			html += ";<br/> ".join(params)
		html += ";<br/>} %s;</div>" % (item.attributes["name"].value)
		
		if len(item.getElementsByTagName('desc')) > 0:
			html +=	"<div id=\"detailcontent\"><div class=\"detailboxdesc\"> Description</div> <div class=\"detailbox\">%s.</div></div>" % (item.getElementsByTagName('desc')[0].childNodes[0].data)
		
		if len(paramdescs) > 0:
			html += "<div id=\"detailcontent\"><div class=\"detailboxdesc\"> Parameters</div><div class=\"detailbox\">"
			html += paramdescs
			html += "</div></div>"
		html += "</div>"


	html += "\t\t<div>\n"
	html += "\t</body>\n"
	html += "\t</html>\n"
	return html



f = open("index.html", "w")
f.write(makeHTML())
f.close()
